package com.pinyoumei.app.activity.wallet;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivityCurrency;
import com.pinyoumei.app.bean.BaseBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

public class HappyCouponToScoreInitActivity extends BaseActivityCurrency implements View.OnClickListener {
    private TextView includeTitle,hcsvHappyNum,hcsvEditAll,hcsvIntegralNum,hcsvSubmit;
    private EditText hcsvIntegralEdit;
    private ImageView includeBack;
    @Override
    protected int layoutId() {
        return R.layout.happy_coupon_score_view;
    }

    @Override
    protected void initView() {
        includeBack = findViewById(R.id.include_back);
        includeBack.setOnClickListener(this);
        includeTitle = findViewById(R.id.include_title);
        hcsvHappyNum = findViewById(R.id.hcsv_happy_num);
        hcsvIntegralEdit = findViewById(R.id.hcsv_integral_edit);
        hcsvEditAll = findViewById(R.id.hcsv_edit_all);
        hcsvEditAll.setOnClickListener(this);
        hcsvIntegralNum = findViewById(R.id.hcsv_integral_num);
        hcsvSubmit = findViewById(R.id.hcsv_submit);
        hcsvSubmit.setOnClickListener(this);
        hcsvIntegralEdit.addTextChangedListener(textWatcher);
        includeTitle.setText("开心券转积分");
    }

    @Override
    protected void initData() {
        happyCouponToScoreInit(SPUtils.getAppUserId(this));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.include_back:
                finish();
                break;
            case R.id.hcsv_submit:
                String hie = hcsvIntegralEdit.getText().toString();
                if (TextUtils.isEmpty(hie)){
                    ToastUtils.showLong("请输入开心劵数量");
                    return;
                }
                happyCouponToScoreSubmit(SPUtils.getAppUserId(this),hie);
                break;
            case R.id.hcsv_edit_all:
                String hhn = hcsvHappyNum.getText().toString();
                hcsvIntegralNum.setText(hhn);
                hcsvIntegralEdit.setText(hhn);
                break;
        }
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() == 0){
                hcsvIntegralNum.setText("0");
            }else{
                hcsvIntegralNum.setText(s.toString());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    private void happyCouponToScoreInit(String user_id){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        OkGo.<String>post(Constants.happyCouponToScoreInit)
                .params("user_id", user_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDailog.dismiss();
                        HappyCouponToScoreInitBean happyCouponInitBean = new Gson().fromJson(response.body(), HappyCouponToScoreInitBean.class);
                        hcsvHappyNum.setText(happyCouponInitBean.getResult().getHappy_coupon());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }

    private void happyCouponToScoreSubmit(String user_id,String transfer_amount){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("transfer_amount",transfer_amount);
        OkGo.<String>post(Constants.happyCouponToScoreSubmit)
                .params("user_id", user_id)
                .params("transfer_amount",transfer_amount)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDailog.dismiss();
                        BaseBean baseBean = new Gson().fromJson(response.body(), BaseBean.class);
                        if (baseBean.getCode() == 1){
                            finish();
                        }
                        ToastUtils.showLong(baseBean.getMsg());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }
}
